/**
 * @file spi_emu.h
 * @author zhaitao (zhaitao.as@outlook.com)
 * @brief emulattion spi
 * @version 0.1
 * @date 2020-07-22
 *
 * @copyright zhaitao.as@outlook.com (c) 2020
 *
 */

#ifndef __SPI_EMU_H__
#define __SPI_EMU_H__

#include "stdint.h"
#include "stdio.h"
#include "stdlib.h"

/**
 * @brief emulattion spi
 * 
 */
struct SpiEmu {

	uint8_t CPHA;	   // clock phase
	uint8_t CPOL;	   // clock polarity
	uint8_t bits;	   // how many bits in one transfer
	uint8_t readBits;  // how many bits in one read
	uint8_t writeBits; // how many bits in one read

	void (*mosi)(uint8_t);	      // mosi pin setting
	int (*miso)(void);	      // read from miso pin
	void (*sck)(uint8_t);	      // sck pin setting
	void (*cs)(uint8_t);	      // mosi pin setting
	void (*delayHalfClock)(void); // do a delay
	void (*toggleSck)(void);      // toggle sck pin
	void (*leadingEdge)(void *);  // 1st edge and holding delay
	void (*trailingEdge)(void *); // 2nd edge and holding delay
};

void seInitial(struct SpiEmu *dev);
void seDeInitial(struct SpiEmu *dev);

uint32_t seTransfer(struct SpiEmu *dev, uint32_t byte_out);
void seWrite(struct SpiEmu *dev, uint8_t *bits, uint32_t v);
uint32_t seRead(struct SpiEmu *dev, uint8_t *bits);

#endif //__SPI_EMU_H__
